package com.htgd.gardenexpo.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;

import java.util.Locale;



public class SelectHomePlaceUtils {
    //加载归属地依赖信息
    public static final PhoneNumberOfflineGeocoder GEOCODER = PhoneNumberOfflineGeocoder.getInstance();
    //查询国内的手机号码归属地
    public static final int COUNTRY_CODE = 86;

    /**
     * 查询手机号码归属地
     *
     * @param phoneNum 手机号码
     * @return 号码归属地
     */
    public static String getPhoneNumAttribution(String phoneNum) {
        if (StringUtils.isNotBlank(phoneNum) && phoneNum.length() == 11) {
            try {
                long phone = Long.parseLong(phoneNum);
                Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
                pn.setCountryCode(COUNTRY_CODE);
                pn.setNationalNumber(phone);
                return GEOCODER.getDescriptionForNumber(pn, Locale.CHINESE);
            } catch (Exception e) {
                return "未知";
            }
        }
        return "未知";
    }
    public static void main(String[] args) {
        String phoneNumAttribution = getPhoneNumAttribution("15222875688");
        System.out.println(phoneNumAttribution);
    }

}
